<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="expectedGoalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55" align="center" type="index" />
      <el-table-column label="标题" align="left" prop="name" show-overflow-tooltip>
				<template #default="scope">
					<el-button link type="primary" @click="handleUpdate(scope.row)" >{{scope.row.name}}</el-button>
				</template>
			</el-table-column>	
      <el-table-column label="预期目标" align="left" prop="goalDescription" show-overflow-tooltip />
      <template #empty>
        <el-empty />
      </template>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预期目标对话框 -->
    <el-dialog :title="title" v-model="open" width="60%" draggable append-to-body>
      <el-form ref="expectedGoalRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="name">
          <el-input v-model="form.name" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="预期目标" prop="goalDescription">
          <el-input v-model="form.goalDescription" type="textarea" :rows="15" placeholder="请输入500字内的描述" show-word-limit/>
        </el-form-item>
        <el-form-item label="附件" prop="files">
          <file-upload v-model="form.files"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ExpectedGoal">
import { listExpectedGoal, getExpectedGoal, delExpectedGoal, addExpectedGoal, updateExpectedGoal } from "@/api/business/expectedGoal"
import { useRoute } from 'vue-router'

const route = useRoute()
const { proxy } = getCurrentInstance()

const expectedGoalList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: route.query.id,
    name: null,
    goalDescription: null,
    files: null,
    status: null,
  },
  rules: {
    name: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    goalDescription: [
      { required: true, message: '预期目标描述不能为空', trigger: 'blur' },
      { max: 500, message: '预期目标描述不能超过500字', trigger: 'blur' }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询预期目标列表 */
function getList() {
  loading.value = true
  listExpectedGoal(queryParams.value).then(response => {
    expectedGoalList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    projectId: route.query.id,
    name: null,
    goalDescription: null,
    files: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("expectedGoalRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加预期目标"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getExpectedGoal(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改预期目标"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["expectedGoalRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateExpectedGoal(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addExpectedGoal(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除预期目标编号为"' + _ids + '"的数据项？').then(function() {
    return delExpectedGoal(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/expectedGoal/export', {
    ...queryParams.value
  }, `expectedGoal_${new Date().getTime()}.xlsx`)
}

getList()
</script>




